﻿using _03_Tank.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _03_Tank
{
    internal class MyTank : Movething
    {

        public bool IsMoving { get; set; }

        public int HP { get; set; }

        private int originalX;
        private int originalY;
        public MyTank(int x, int y, int speed, Direction dir)
        {
            IsMoving = false;
            BitmapDown = Resources.MyTankDown;
            BitmapUp = Resources.MyTankUp;
            BitmapLeft = Resources.MyTankLeft;
            BitmapRight = Resources.MyTankRight;

            X = x;
            Y = y;
            Speed = speed;
            Dir = dir;
            HP = 4;

            originalX = x;
            originalY = y;
        }

        public override void Update()
        {
            MoveCheck();
            Move();
            base.Update();
        }

        private void MoveCheck()
        {
            if (Dir == Direction.Up)
            {
                if (Y - Speed < 0)
                {
                    IsMoving = false; return;
                }
            }
            else if (Dir == Direction.Down)
            {
                if (Y + Height + Speed > 450)
                {
                    IsMoving = false; return;
                }
            }
            else if (Dir == Direction.Left)
            {
                if (X - Speed < 0)
                {
                    IsMoving = false; return;
                }
            }
            else if (Dir == Direction.Right)
            {
                if (X + Width + Speed > 450)
                {
                    IsMoving = false; return;
                }
            }


            Rectangle rectangle = GetRectangle();
            switch (Dir)
            {
                case Direction.Up:
                    rectangle.Y -= Speed;
                    break;
                case Direction.Down:
                    rectangle.Y += Speed;
                    break;
                case Direction.Left:
                    rectangle.X -= Speed;
                    break;
                case Direction.Right:
                    rectangle.X += Speed;
                    break;
            }

            if (GameObjectManager.IsCollidedWall(rectangle) != null)
            {
                IsMoving = false;
                return;
            }

            if (GameObjectManager.IsCollidedSteel(rectangle) != null)
            {
                IsMoving = false;
                return;
            }

            if (GameObjectManager.IsCollidedBoss(rectangle)) 
            {
                IsMoving = false;
                return;
            }
        }

        private void Move()
        {
            if (!IsMoving) return;

            switch (Dir)
            {
                case Direction.Up:
                    Y -= Speed;
                    break;
                case Direction.Down:
                    Y += Speed;
                    break;
                case Direction.Left:
                    X -= Speed;
                    break;
                case Direction.Right:
                    X += Speed;
                    break;
                default:
                    break;
            }
        }

        public void KeyDown(KeyEventArgs args)
        {
            switch (args.KeyCode)
            {
                case Keys.W:
                    Dir = Direction.Up;
                    IsMoving = true;
                    break;
                case Keys.S:
                    Dir = Direction.Down;
                    IsMoving = true;
                    break;
                case Keys.A:
                    Dir = Direction.Left;
                    IsMoving = true;
                    break;
                case Keys.D:
                    Dir = Direction.Right;
                    IsMoving = true;
                    break;
                case Keys.Space:
                    Attack();
                    break;
            }
        }

        private void Attack()
        {
            int x = X;
            int y = Y;
            switch (Dir)
            {
                case Direction.Up:
                    x = x + Width / 2;
                    break;
                case Direction.Down:
                    x = x + Width / 2;
                    y += Height;
                    break;
                case Direction.Left:
                    y = y + Height / 2;
                    break;
                case Direction.Right:
                    x += Width;
                    y = y + Height / 2;
                    break;
                
            }
            GameObjectManager.CreateBullet(x, y, Tag.MyTank, Dir);
        }

        public void KeyUp(KeyEventArgs args)
        {
            switch (args.KeyCode)
            {
                case Keys.W:
                    
                    IsMoving = false;
                    break;
                case Keys.S:
                    
                    IsMoving = false;
                    break;
                case Keys.A:
                    
                    IsMoving = false;
                    break;
                case Keys.D:
                    
                    IsMoving = false;
                    break;
            }

        }

        public void TakeDamage()
        {
            HP--;
            if (HP <= 0)
            {
                X = originalX;
                Y = originalY;
                HP = 4;
            }
        }
    }
}
